import { Tools } from 'src/app/common/tools/tools';
import { ExceptionHandler } from 'src/app/handler/exception.handler';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { BaseService, DataRegisterService, PageService } from 'src/app/services';
import { EventManagerService } from 'src/app/services/event-manager.service';
import { OrganizationService } from 'src/app/services/organization.service';
import { UploadService } from 'src/app/services/upload.service';
import { EngineService } from '../../../services/engine.service';
import { BaseFieldComponent } from '../base-field/base-field.component';
import { debounceTime } from 'rxjs/operators';
import { PermitConfig } from '../../../models';

@Component({
  selector: 'zepride-doc-assembly',
  templateUrl: './doc-assembly.component.html',
  styleUrls: ['./doc-assembly.component.less']
})
export class DocAssemblyComponent extends BaseFieldComponent implements OnInit, OnDestroy {
  docList: {
    name: string;
    component: any;
    tmpComItemId: string;
    id: string;
    isExtend?: boolean;
    fileList?: any[];
    color?: string;
  }[] = [];

  permitConfig: PermitConfig[] = [
    { mode: 'query', permitId: '查看', permitName: '', authObjectId: '' },
    { mode: 'edit', permitId: '编辑', permitName: '', authObjectId: '' }
  ];

  constructor(
    protected engineService: EngineService,
    protected baseSvr: BaseService,
    protected eventSvr: EventManagerService,
    protected orgSvr: OrganizationService,
    public uploadService: UploadService,
    protected msg: NzMessageService,
    protected pageSvr: PageService,
    protected dataRegisterService: DataRegisterService
  ) {
    super(engineService, baseSvr, eventSvr, orgSvr, uploadService, msg, dataRegisterService);
  }

  ngOnInit() {
    this.docList = this.componentData.extends.fields;
    this.getFileList();

    this.registerModelChange();
  }

  getFileList() {
    if (!this.docList) {
      return;
    }
    for (const item of this.docList) {
      const fsData = this.dataModel[item.component.bindField];
      if (fsData instanceof Array) {
        item.fileList = [];
        fsData.forEach(x => {
          item.fileList = item.fileList.concat(x.fileList);
        });
      }
      else {
        item.fileList = [fsData];
      }

      item.color = Tools.getRandColor();
    }
  }

  extend(item) {
    item.isExtend = !item.isExtend;
  }

  async makePDF() {
    try {
      this.getFileList();
      let postData: string[] = [];
      const list = this.docList.filter(x => x.fileList);
      list.map(x => {
        postData = postData.concat(x.fileList.map(n => n.url));
      });

      if (postData.length > 0) {
        const result = await this.pageSvr.makeMergePDF(postData);
        window.open(result.data);
      }

    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

  registerModelChange() {
    this.modelChange$ = this.engineService.dataModelChange$.pipe(
      debounceTime(200)
    ).subscribe(data => {
      if (data.identifyId !== this.componentData.identifyId && data.selector == 'files-upload') {
        this.getFileList();
      }
    });
  }


  ngOnDestroy() {
    if (this.modelChange$) {
      this.modelChange$.unsubscribe();
    }
  }

  download(url) {
    window.open(url);
  }

}
